/****************************************************************************

MIT License

Copyright (c) 2021 唐佐林
WeChat : delphi_tang
EMail: delphi_tang@dt4sw.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*****************************************************************************/
const char* c_math_py = 
{
"import math\r\n"
"\r\n"
"a = 1.6\r\n"
"b = 0.25\r\n"
"\r\n"
"print(dir(math))\r\n"
"\r\n"
"print(a / b)\r\n"
"print(a ** b)\r\n"
"print(math.floor(9.85))\r\n"
"print(math.fmod(9.85, 9))\r\n"
"print(math.modf(9.85))\r\n"
"print(math.fabs(-0.05))\r\n"
"print(math.e)\r\n"
"print(math.pi)\r\n"
"print(math.isfinite(1.11))\r\n"
"print(math.isinf(1.11))\r\n"
"print(math.isnan(float(\'nan\')))\r\n"
};

const char* c_pwm_py = 
{
"import gpio\r\n"
"import pwm\r\n"
"import os\r\n"
"\r\n"
"is1_gpio = 7\r\n"
"pwm_port = 1\r\n"
"pwm_gpio = 8\r\n"
"pwm_on = False\r\n"
"\r\n"
"def is1_callback(arg) :\r\n"
"    print(\'is1_callback() is invoked\')\r\n"
"    cur_tick = os.get_tick()\r\n"
"\r\n"
"    if cur_tick - arg[0] > 30:\r\n"
"        global pwm_on\r\n"
"        \r\n"
"        if not pwm_on :\r\n"
"            pwm_on = True\r\n"
"            arg[0] = cur_tick\r\n"
"            print(\'to beep\')\r\n"
"\r\n"
"def init() :\r\n"
"    gpio.gpio_init(is1_gpio)\r\n"
"    \r\n"
"    func = gpio.query_func_value(is1_gpio, \'gpio\')\r\n"
"    \r\n"
"    gpio.set_func(is1_gpio, func)\r\n"
"    \r\n"
"    pull = gpio.query_pull_value(\'down\')\r\n"
"    \r\n"
"    gpio.set_pull(is1_gpio, pull)   \r\n"
"    gpio.set_dir(is1_gpio, gpio.dir_in)\r\n"
"    gpio.set_isr_mode(is1_gpio, gpio.rise_high)\r\n"
"    gpio.register_isr_func(is1_gpio, is1_callback, [os.get_tick()])\r\n"
"    \r\n"
"    gpio.gpio_init(pwm_gpio)\r\n"
"    \r\n"
"    func = gpio.query_func_value(pwm_gpio, \'pwm1_out\')\r\n"
"    gpio.set_func(pwm_gpio, func)\r\n"
"    \r\n"
"    gpio.set_dir(pwm_gpio, gpio.dir_out)\r\n"
"    \r\n"
"    pwm.pwm_init(pwm_port)\r\n"
"    \r\n"
"init()\r\n"
"\r\n"
"while True:     \r\n"
"    if pwm_on :\r\n"
"       print(\'beeping...\')\r\n"
"       pwm.start(pwm_port, 50, 4000)\r\n"
"       os.sleep(3)\r\n"
"       pwm.stop(pwm_port)\r\n"
"       pwm_on = False\r\n"
"    else :\r\n"
"       os.usleep(10000)  \r\n"
};


const char* c_main_py = 
{
"import gpio\r\n"
"import os\r\n"
"\r\n"
"current = 0\r\n"
"\r\n"
"gpio.gpio_init(12)\r\n"
"\r\n"
"func = gpio.query_func_value(12, \'gpio\')                        \r\n"
"                                                               \r\n"
"gpio.set_func(12, func)                                         \r\n"
"\r\n"
"gpio.set_dir(12, gpio.dir_out)                                  \r\n"
"\r\n"
"\r\n"
"while True :\r\n"
"    if current == 0 :\r\n"
"        current = 1\r\n"
"    else :\r\n"
"        current = 0\r\n"
"    \r\n"
"    gpio.set_output(12, current) \r\n"
"    \r\n"
"    os.sleep(2)\r\n"
"\r\n"
"gpio.gpio_deinit(12)\r\n"
"\r\n"
};

const char* c_gpio_py = 
{
"import gpio"                                                      "\r\n" 
"import os"                                                        "\r\n"  
                                                                   "\r\n"
"led = 7"                                                          "\r\n"
"button = 11"                                                      "\r\n"                 
                                                                   "\r\n"
"led_on = False"                                                   "\r\n"
                                                                   "\r\n"
"def button_callback(arg) :"                                       "\r\n"
"    cur_tick = os.get_tick()"                                     "\r\n"
                                                                   "\r\n"
"    if cur_tick - arg[0] > 20:"                                   "\r\n"
"        global led_on"                                            "\r\n"
"        if led_on :"                                              "\r\n"
"            gpio.set_output(led, 0)"                              "\r\n"
"            led_on = False"                                       "\r\n"
"            print('LED is OFF!')"                                 "\r\n"
"        else :"                                                   "\r\n"
"            gpio.set_output(led, 1)"                              "\r\n"
"            led_on = True"                                        "\r\n"
"            print('LED is ON!')"                                  "\r\n"
                                                                   "\r\n"
"        arg[0] = cur_tick"                                        "\r\n"
                                                                   "\r\n"
                                                                   "\r\n"
"gpio.gpio_init(led)"                                              "\r\n"
"gpio.gpio_init(button)"                                           "\r\n"
                                                                   "\r\n"
"func = gpio.query_func_value(led, 'gpio')"                        "\r\n"
                                                                   "\r\n"
"gpio.set_func(led, func)"                                         "\r\n"
"gpio.set_dir(led, gpio.dir_out)"                                  "\r\n"
"gpio.set_output(led, 0)"                                          "\r\n"
                                                                   "\r\n"
"func = gpio.query_func_value(button, 'gpio')"                     "\r\n"
                                                                   "\r\n"
"gpio.set_func(button, func)"                                      "\r\n"
"gpio.set_dir(button, gpio.dir_in)"                                "\r\n"
                                                                   "\r\n"
"pull_up = gpio.query_pull_value('up')"                            "\r\n"
                                                                   "\r\n"
"gpio.set_pull(button, pull_up)"                                   "\r\n"
"gpio.set_isr_mode(button, gpio.fall_low)"                         "\r\n"
"gpio.register_isr_func(button, button_callback, [os.get_tick()])" "\r\n"
                                                                   "\r\n"
"while True:"                                                      "\r\n"
"    os.sleep(1)"                                                  "\r\n"
                                                                   "\r\n"
"gpio.gpio_deinit(led)"                                            "\r\n"
"gpio.gpio_deinit(button)"                                         "\r\n"
};

const char* c_i2c_py = 
{
"import gpio"                                                      "\r\n"
"import i2c"                                                       "\r\n"
"import os"                                                        "\r\n"
                                                                   "\r\n"
"sht30_addr = 0x44 << 1"                                           "\r\n"
                                                                   "\r\n"
"def cal_t(t):"                                                    "\r\n"
"    t = t & ~0x3"                                                 "\r\n"
"    t = t * 175 // 65536 - 45"                                    "\r\n"
"    return t"                                                     "\r\n"
                                                                   "\r\n"
"def cal_h(h):"                                                    "\r\n"
"    h = h & ~0x3"                                                 "\r\n"
"    h = h * 100 // 65536"                                         "\r\n"
"    return h"                                                     "\r\n"
                                                                   "\r\n"
"def init():"                                                      "\r\n"
"    gpio.gpio_init(0)"                                            "\r\n"
"    func = gpio.query_func_value(0, \'I2C1_SDA\')"                "\r\n"
"    gpio.set_func(0, func)"                                       "\r\n"
                                                                   "\r\n"
"    gpio.gpio_init(1)"                                            "\r\n"
"    func = gpio.query_func_value(1, \'I2C1_SCL\')"                "\r\n"
"    gpio.set_func(1, func)"                                       "\r\n"
                                                                   "\r\n"
"    i2c.i2c_init(1, 400000)"                                      "\r\n"
"    i2c.write(1, sht30_addr, [0x22, 0x36])"                       "\r\n"
                                                                   "\r\n"
"def read():"                                                      "\r\n"
"    r , d = i2c.write_read(1, sht30_addr, [0xE0, 0x00], 6)"       "\r\n"
                                                                   "\r\n"
"    if r == 0:"                                                   "\r\n"
"        t = (d[0] << 8) | d[1]"                                   "\r\n"
"        h = (d[3] << 8) | d[4]"                                   "\r\n"
"        return cal_t(t), cal_h(h)"                                "\r\n"
"    else:"                                                        "\r\n"
"        return None, None"                                        "\r\n"
                                                                   "\r\n"
"init()"                                                           "\r\n"
                                                                   "\r\n"
"while True:"                                                      "\r\n"
"    t , h = read()"                                               "\r\n"
                                                                   "\r\n"
"    if t != None:"                                                "\r\n"
"        print(\'temperature = \' + str(t))"                       "\r\n"
                                                                   "\r\n"
"    if h != None:"                                                "\r\n"
"        print(\'humidity = \' + str(h))"                          "\r\n"
                                                                   "\r\n"
"    os.sleep(3)"                                                  "\r\n"
};

const char* c_test_py = 
{
"from another import Person"                            "\r\n"
                                                        "\r\n"
"def SayHello(n) :"                                     "\r\n"
"    for i in range(n) :"                               "\r\n"
"        print(\"Hello World: \" + str(i))"             "\r\n"
                                                        "\r\n"
"a = 4"                                                 "\r\n"
"b = 5"                                                 "\r\n"
                                                        "\r\n"
"print(\'a + b = \', a + b)"                            "\r\n"
                                                        "\r\n"
"SayHello(6)"                                           "\r\n"
                                                        "\r\n"
"s1 = \'Delphi\'"                                       "\r\n"
"s2 = \'Tang\'"                                         "\r\n"
                                                        "\r\n"
"if s1 == s2 :"                                         "\r\n"
"    print(\'Equal\')"                                  "\r\n"
"else :"                                                "\r\n"
"    print(\'No Equal\')"                               "\r\n"
                                                        "\r\n"
"print(\'s1 + s2 = \', s1 + \' \' + s2)"                "\r\n"
                                                        "\r\n"
"p = Person(\'Delphi Tang\')"                           "\r\n"
"print(p)"                                              "\r\n"
"p.SayHello();"                                         "\r\n"
};

const char* c_another_py = 
{
"class Person:"                                         "\r\n"
"    def __init__(self, name):"                         "\r\n"
"        self.name = name"                              "\r\n"
                                                        "\r\n"
"    def SayHello(self):"                               "\r\n"
"        print(\'My name is:\', self.name)"             "\r\n"
};